package ew_send_sms;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Sending_sms {
	
	static public void get_authority(String room_id,String Appliance_id) throws IOException, InterruptedException
	{
		//while(true)
		//{
			BufferedReader br=null;
			//BufferedReader br2=null;
			PrintWriter pr=null;
			try{
			File f1=new File("Authority_Database.txt");
			File f2=new File("Log_sms_sent.txt");
			br=new BufferedReader(new FileReader(f1));
			pr=new PrintWriter(new FileWriter(f2,true));
			//br2=new BufferedReader(new FileReader(f2));
			}catch(FileNotFoundException fnf)
			{
				System.out.println("File Named: Authority Database was not found.");
				return;
			}
			String str;
			while((str=br.readLine())!=null)
			{
				//System.out.println(str);
				String Authority_Name=null;
				String number=null;
				String message;
				String[] arr1=str.split(" ");
				//System.out.println(arr1[0]);
				if( room_id.equalsIgnoreCase(arr1[0])==true)
				{
					Authority_Name=arr1[1]+" "+arr1[2]+" "+arr1[3];
					number=arr1[4];
				message="This is to inform you that electricity wastage has been detected in room no. "+room_id+". Appliance with the wastage has ID as "+Appliance_id+". Being the concerned authority("+Authority_Name+") for this room, we request you to switch off the appliance ASAP.\nBy:-IIIT Delhi";
				System.out.println(message);
				send_sms(number,message);
				pr.write("Update sms_sent set sent_time="+now()+", sent='y' where sms sent to "+Authority_Name+" with Room_ID "+room_id+"\n");
            	//pr.println();
				break;
			}
		}
			pr.close();
			br.close();
	}
	//}
	//The below written code has been taken from Siddharth sir
	public static void send_sms(String number, String message) throws InterruptedException
	{
		String arr[] =message.split("\\s");
		String reformed ="";
		for(int i = 0;i<arr.length;i++)
		{
			reformed = reformed +"%20" + arr[i];
		}
		System.out.println(reformed);
		System.out.println("Query formed:\nwget -o smssent -r --no-check-certificate https://admin:99admin11@192.168.1.175/admin/forms/sms-send.html?number="+number.toString()+"&smsencode="+reformed+"&send=Send");
		/*try {
			Runtime.getRuntime().exec("wget -o smssent -r --no-check-certificate https://admin:99admin11@192.168.1.175/admin/forms/sms-send.html?number="+number.toString()+"&smsencode="+reformed+"&send=Send").waitFor();
		} catch (IOException e) {
			
			e.printStackTrace();
		}*/
	}
	//Ref:http://stackoverflow.com/questions/833768/java-code-for-getting-current-time
	static public String now(){
	Calendar cal = Calendar.getInstance();
    cal.getTime();
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    return (String)sdf.format(cal.getTime());
    
	}
	
	static public void main(String[] args) throws IOException, InterruptedException
	{
		get_authority("CR3","F2");
		get_authority("CR2","A2");
		get_authority("CR1","B2");
		
	}
}
